package com.tj.collection.db.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.tj.collection.db.dao.ClReceiverDao;
import com.tj.collection.db.entity.ClReceiverEntity;
import com.tj.collection.db.entity.business.ClLanguageOrderCountEntity;
import com.tj.collection.db.entity.business.WrongPaidStatusCollectionOrderVo;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2019-09-03
 * Time: 10:28
 */
@Repository
public class ClReceiverDaoImpl extends AbstractBaseMapper<ClReceiverEntity> implements ClReceiverDao {

    @Override
    public ClReceiverEntity selectLastByBillNo(String billNo) {
        if (billNo == null || billNo.length() == 0) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("billNo", billNo);
        return getSqlSession().selectOne(this.getStatement("selectLastByBillNo"), map);
    }

    @Override
    public ClReceiverEntity selectLastByBillNoAndNotPay(String billNo) {
        if (billNo == null || billNo.length() == 0) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("billNo", billNo);
        return getSqlSession().selectOne(this.getStatement("selectLastByBillNoAndNotPay"), map);
    }

    @Override
    public List<ClReceiverEntity> selectListByBillNo(String billNo) {
        if (billNo == null || billNo.length() == 0) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("billNo", billNo);
        return getSqlSession().selectList(this.getStatement("selectListByBillNo"), map);
    }

    @Override
    public ClReceiverEntity selectLastByOrderNo(String orderNo) {
        if (orderNo == null || orderNo.length() == 0) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return getSqlSession().selectOne(this.getStatement("selectLastByOrderNo"), map);
    }

    @Override
    public List<ClReceiverEntity> selectListByBillNos(List<String> billNos) {
        if (CollectionUtils.isEmpty(billNos)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("billNos", billNos);
        return getSqlSession().selectList(this.getStatement("selectListByBillNos"), map);
    }

    @Override
    public List<ClReceiverEntity> selectListByOrderNos(List<String> orderNos) {
        if (CollectionUtils.isEmpty(orderNos)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("orderNos", orderNos);
        return getSqlSession().selectList(this.getStatement("selectListByOrderNos"), map);
    }

    @Override
    public ClReceiverEntity selectByCollectionNo(String collectionNo) {
        if (collectionNo == null || collectionNo.length() == 0) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNo", collectionNo);
        return getSqlSession().selectOne(this.getStatement("selectByCollectionNo"), map);
    }

    @Override
    public ClReceiverEntity selectUnsettledByCollectionNo(String collectionNo) {
        if (collectionNo == null || collectionNo.length() == 0) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNo", collectionNo);
        return getSqlSession().selectOne(this.getStatement("selectUnsettledByCollectionNo"), map);
    }

    @Override
    public List<ClReceiverEntity> selectListByUserCode(String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userCode);
        return getSqlSession().selectList(this.getStatement("selectListByUserCode"), map);
    }

    @Override
    public void updateOverdueDays() {
        Map<String, Object> map = new HashMap<>();
        getSqlSession().selectOne(this.getStatement("updateOverdueDays"), map);
    }

    @Override
    public List<ClReceiverEntity> selectListByCreateDtAndStatusAndMerchantCode(Date date, Integer status, String merchantCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("date", date);
        map.put("status", status);
        map.put("merchantCode", merchantCode);
        return getSqlSession().selectList(this.getStatement("selectListByCreateDtAndStatusAndMerchantCode"), map);
    }

    @Override
    public void updateStatusByCollectionNos(List<String> collectionNos, Integer status) {
        if (collectionNos == null || collectionNos.size() == 0) {
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        map.put("collectionNos", collectionNos);
        getSqlSession().update(this.getStatement("updateStatusByCollectionNos"), map);
    }

    @Override
    public Integer updateStatusByBatchNo(String batchNo, Integer status) {
        if (StringUtils.isEmpty(batchNo)) {
            return 0;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        map.put("batchNo", batchNo);
        return getSqlSession().update(this.getStatement("updateStatusByBatchNo"), map);
    }

    @Override
    public List<ClReceiverEntity> selectByCollectionNos(List<String> collectionNos) {
        Map<String, Object> map = new HashMap<>();
        map.put("collectionNos", collectionNos);
        return getSqlSession().selectList(this.getStatement("selectByCollectionNos"), map);
    }

    @Override
    public List<ClReceiverEntity> selectNotEndListByPage(Integer startIndex, Integer endIndex) {
        Map<String, Object> map = new HashMap<>();
        map.put("startIndex", startIndex);
        map.put("pageSize", endIndex);
        return getSqlSession().selectList(this.getStatement("selectNotEndListByPage"), map);
    }

    @Override
    public List<ClReceiverEntity> selectEndListByPageAndOverdueDays(Integer startIndex, Integer endIndex) {
        Map<String, Object> map = new HashMap<>();
        map.put("startIndex", startIndex);
        map.put("pageSize", endIndex);
        return getSqlSession().selectList(this.getStatement("selectEndListByPageAndOverdueDays"), map);
    }

    @Override
    public List<WrongPaidStatusCollectionOrderVo> getWrongPaidStatusCollectionOrders() {
        return getSqlSession().selectList(this.getStatement("getWrongPaidStatusCollectionOrders"));
    }

    @Override
    public List<ClLanguageOrderCountEntity> selectLanguageOrderCountList(String merchantCode, Integer overdueDays) {
        if (StringUtils.isEmpty(merchantCode) || overdueDays == null) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("merchantCode", merchantCode);
        map.put("overdueDays", overdueDays);
        return getSqlSession().selectList(this.getStatement("selectLanguageOrderCountList"), map);
    }

    /**
     * 查询今日待分单和已分单的订单数汇总
     *
     * @param merchantCode
     * @param overdueDays
     * @return
     */
    @Override
    public List<ClLanguageOrderCountEntity> selectCumulativeLanguageOrderCountList(String merchantCode, Integer overdueDays, List<Integer> adminIdList) {
        if (StringUtils.isEmpty(merchantCode) || overdueDays == null) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("merchantCode", merchantCode);
        map.put("overdueDays", overdueDays);
        map.put("adminIdList", adminIdList);
        return getSqlSession().selectList(this.getStatement("selectCumulativeLanguageOrderCountList"), map);
    }

    @Override
    public List<Integer> selectOverdueDaysListByMerchantCode(String merchantCode) {
        if (StringUtils.isEmpty(merchantCode)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("merchantCode", merchantCode);
        List<Map<String, Object>> map2 = getSqlSession().selectList(this.getStatement("selectOverdueDaysListByMerchantCode"), map);
        if (CollectionUtils.isEmpty(map2)) {
            return new ArrayList<>();
        }
        List<Integer> overdueDays = map2.stream().map(item -> (Integer) item.get("overdueDays")).collect(Collectors.toList());
        return overdueDays;
    }

    @Override
    public List<ClReceiverEntity> selectReceiverListByMerchantCodeAndOverdueDays(String merchantCode, Integer overdueDays, String userLanguage, Integer status, Page page) {
        if (StringUtils.isEmpty(merchantCode) || overdueDays == null) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("merchantCode", merchantCode);
        map.put("overdueDays", overdueDays);
        map.put("userLanguage", userLanguage);
        map.put("status", status);
        map.put("page", page);
        return getSqlSession().selectList(this.getStatement("selectReceiverListByMerchantCodeAndOverdueDays"), map);
    }

    @Override
    public Integer updateReceiverActiveDate(String billNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("billNo", billNo);
        map.put("lastActiveDate", new Date());
        return getSqlSession().update(this.getStatement("updateReceiverActiveDate"), map);
    }
}
